//
//  YCPopupPresentAnimation.m
//  YCAlertController
//
//  Created by Yochia on 2017/3/9.
//  Copyright © 2017年 Team Brutal. All rights reserved.
//

#import "YCPopupPresentAnimation.h"
#import "YCPopupController.h"
#import "YCPopupView.h"

@implementation YCPopupPresentAnimation

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    YCPopupController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    toVC.view.alpha = 0;
    if (toVC.popupView && [toVC.popupView respondsToSelector:@selector(popupViewAppearAnimationOrigin)]) {
        [toVC.popupView popupViewAppearAnimationOrigin];
    }

    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration
                     animations:^{
                         toVC.view.alpha = 1;
                         if (toVC.popupView && [toVC.popupView respondsToSelector:@selector(popupViewAppearAnimationDestination)]) {
                             [toVC.popupView popupViewAppearAnimationDestination];
                         }
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;
}

@end
